import React from 'react'
import {Tooltip} from 'antd'
// import moment from "moment";
/**
 * @description: 通过cookieName获取值
 * @param {string} CookieName
 * @return {string}
 */
export function getCookie(name: string) {
	let cStart
	let cEnd
	if (document.cookie.length > 0) {
		cStart = document.cookie.indexOf(`${name}=`)

		if (cStart !== -1) {
			cStart = cStart + name.length + 1
			cEnd = document.cookie.indexOf(';', cStart)
			if (cEnd === -1) {
				cEnd = document.cookie.length
			}
			return decodeURIComponent(document.cookie.substring(cStart, cEnd))
		}
	}
	return ''
}

/**
 * @description: 清洗data数据
 * @param {any} data 要清洗的数据
 * @param {'base'|'complex'} useMode 清洗模式
 * @return {any}
 */
export function cleanData<T = any>(
	data: any,
	useMode: 'base' | 'complex' = 'base',
): T {
	const newData: any = {}
	Object.entries(data).forEach(([keyname, value]: any) => {
		// 基本模式
		if (useMode === 'base') {
			if (value === 0 || value) newData[keyname] = value
		}
		if (useMode === 'complex') {
			if (value && typeof value === 'object' && Object.keys(value).length)
				newData[keyname] = value
			else if (value === 0 || value) newData[keyname] = value
			else;
		}
	})
	return newData
}

/**
 * @description: 获取数据类型
 * @param {any} data
 * @return {string}
 */
export function types(data: any): string {
	const type = Object.prototype.toString.call(data).toLowerCase()
	const typeMap = {
		'[object object]': 'object',
		'[object array]': 'array',
		'[object string]': 'string',
		'[object number]': 'number',
		'[object null]': 'null',
		'[object undefined]': 'undefined',
		'[object boolean]': 'boolean',
		'[object function]': 'function',
	} as any
	return typeMap[type] || '[unknown]'
}

/**
 * @description: 测量文字长度，分为两种模式，base模式下直接返回string.length，complex模式下会计算字符范围进行返回
 * @param {string} str 要测量的字符串
 * @param {'base' | 'complex'} mode 测量模式
 * @return {number} 字符长度
 */
function measureStrLen(str: string, mode: 'base' | 'complex' = 'base') {
	let len = 0
	if (mode === 'base') return str.length
	if (mode === 'complex') {
		for (let i = 0; i < str.length; i += 1) {
			if (str.charCodeAt(i) > 128) len += 2
			else len += 1
		}
	}
	return len
}

// 字符串装饰函数，提供给ellipsisText使用
function wrapper(before: string, after: string, isEllipsis: boolean = false) {
	if (isEllipsis) return <Tooltip title={before}>{after}</Tooltip>
	return <>{before}</>
}
/**
 * @description: 字符截断，分为两种模式，base模式下根据string.length进行截断，complex模式下会计算字符范围进行截断
 * @param {string} text 输入文字
 * @param {number} limit 长度限制，默认10
 * @param {string} fill 填充字符，默认 ...
 * @param {'base' | 'complex'} mode 测量模式
 * @return {string}
 */
export function ellipsisText<T = any>(
	text: string = '',
	limit = 10,
	fill = '...',
	mode: 'base' | 'complex' = 'base',
	_wrapper:
		| null
		| ((before: string, after: string, isEllipsis?: boolean) => T) = null,
) {
	const _text = text.toString()
	let cacheText = _text
	let len = measureStrLen(_text, mode)
	const fillLen = measureStrLen(fill, mode)
	let pos = _text.length
	const cwrapper = _wrapper || wrapper
	if (len <= limit) return cwrapper(_text, _text, false)
	if (len > limit && fillLen >= limit) return cwrapper(_text, fill, true)
	while (len + fillLen > limit) {
		const dlen = len + fillLen - limit
		pos -= Math.ceil(dlen / 2)
		cacheText = cacheText.slice(0, pos)
		len = measureStrLen(cacheText, mode)
	}
	return cwrapper(_text, `${cacheText}${fill}`, true)
}

export interface UserInfo {
	adminName?: string
	departId?: {
		level_0: string
		level_1: string
		level_2: string
		level_3: string
		level_4: string
	}
	dept?: string
	email?: string
	emplId?: string
	headImg?: string
	jobcode?: string
	jobcodeDescr?: string
	opName?: string
	opNameCn?: string
	phone?: string
	username?: string
}

/**
 * @description: 获取用户信息
 * @return {UserInfo}
 */
export function getUserInfo() {
	let res: UserInfo = {}
	try {
		res = JSON.parse(getCookie('userInfo') || '{}') || {}
	} catch (error) {
		console.error('getUserInfo:', error)
	}
	return res
}
/**
 * 通过锚点跳转
 * @param anchorName
 */
export function scrollToAnchor(anchorName: string) {
	if (anchorName) {
		const anchorElement = document.getElementById(anchorName)
		if (anchorElement) {
			anchorElement.scrollIntoView()
		}
	}
}

export default {
	getCookie,
	types,
	ellipsisText,
	getUserInfo,
	cleanData,
	scrollToAnchor,
}
